<?php
/**
 * Created by PhpStorm.
 * User: hpp
 * Date: 2019/12/17
 * Time: 11:56
 */
namespace api\department\controller;
use think\Db;
use cmf\controller\PublicController;
use app\common\model\RlNodeModel;
use app\common\model\RlTypeModel;
use app\common\model\RlBmNodeRelModel;
use api\common\model\RlEtCompanyModel;
use api\common\model\RlEtCustomerModel;
/**
 * 节点配置
 * @package 
 */
class IndexController extends PublicController
{
    protected $auth = '*';
    public function initialize()
    {
        $this->rlNodeModel = new RlNodeModel;
        $this->rlTypeModel = new RlTypeModel;
        $this->rlBmNodeRelModel = new RlBmNodeRelModel;
        $this->RlEtCompanyModel = new RlEtCompanyModel;
        $this->RlEtCustomerModel = new RlEtCustomerModel;
    }
    //获取分公司下经销商数据
    public function jxs()
    {
        $keyword = $this->request->param("keyword",'');
        $jxss = $this->RlEtCompanyModel->where('fgs',$this->user['bm_code'])->column('jxsbm');
        if($keyword)
        {
            $data = $this->RlEtCustomerModel->where('jxsbm','in',$jxss)->where('jxs_mc','like','%'.$keyword.'%')->select()->toArray();
        }else{
            $data = $this->RlEtCustomerModel->where('jxsbm','in',$jxss)->select()->toArray();
        }
        $this->success('请求成功',$data);
    }
    //基础节点设置页面，返回类型信息
    public function install()
    {
        $type = $this->rlTypeModel->select()->toArray();
        //获取总平台的类型信息
        $type_id = $this->rlBmNodeRelModel->where('bm',1)->column('type_id');

        $data['type_id'] = $type_id;
        $data['type'] = $type;
        foreach ($data['type'] as $key => $value) {
            if(in_array($value['type_id'], $type_id)){
                $data['type'][$key]['up'] = 1;
            }else{
                $data['type'][$key]['up'] = 0;
            }
        }
        $this->success('请求成功',$data);
    }
    //基础节点设置页面，返回节点信息
    public function installnodeindex()
    {
        $type_id = $this->request->param("type_id", 0, 'intval');
        $jxsbm = $this->request->param("jxsbm", 0, 'intval');
        if (empty($type_id)) {
            $this->error(lang("请选择分类"));
        }
        $node = $this->rlNodeModel->where('type_id',$type_id)->select()->toArray();
        //获取总用户的对应节点信息
        $rlBmNodeRel = $this->rlBmNodeRelModel->where('bm' , 1)->where('type_id',$type_id)->find();
        $node_ids = explode(",", $rlBmNodeRel['node_id']);
        // $data['node_ids'] = $node_ids;
        $data['node'] = $node;
        $data['type_id'] = $type_id;
        //如果是分公司
        if($this->user['gw'] > 2&&$this->user['gw'] < 7)
        {
            $fgs_type_id = $this->rlBmNodeRelModel->where('bm',$this->user['bm_code'].'.'.$jxsbm)->where('type_id',$type_id)->find();
            $fgs_type_ids = explode(",", $fgs_type_id['node_id']);
            //分公司自己选的节点，可以控制开关
            foreach ($data['node'] as $key => $value) {
                if(in_array($value['node_id'], $fgs_type_ids)){
                    $data['node'][$key]['on'] = 1;
                }else{
                    $data['node'][$key]['on'] = 0;
                }
            }
            //总部选的节点不能控制开关
            foreach ($data['node'] as $key => $value) {
                if(in_array($value['node_id'], $node_ids)){
                    $data['node'][$key]['on'] = 2;
                }else{
                    // $data['node'][$key]['on'] = 0;
                }
            }
            $this->success('请求成功',$data);
        }
        //如果是经销商
        if($this->user['gw'] == 0)
        {
            $fgs = $this->RlEtCompanyModel->where('jxsbm',$this->user['bm_code'])->value('fgs');
            $fgs_type_id = $this->rlBmNodeRelModel->where('bm',$fgs.'.'.$this->user['bm_code'])->where('type_id',$type_id)->find();
            $jxs_type_id = $this->rlBmNodeRelModel->where('bm',$this->user['bm_code'])->where('type_id',$type_id)->find();
            $fgs_type_ids = explode(",", $fgs_type_id['node_id']);
            $jxs_type_ids = explode(",", $jxs_type_id['node_id']);
            $node_ids = array_merge($node_ids,$fgs_type_ids);
            //经销商自己选的节点，可以控制开关
            foreach ($data['node'] as $key => $value) {
                if(in_array($value['node_id'], $jxs_type_ids)){
                    $data['node'][$key]['on'] = 1;
                }else{
                    $data['node'][$key]['on'] = 0;
                }
            }
            //总部，分公司选的节点不能控制开关
            foreach ($data['node'] as $key => $value) {
                if(in_array($value['node_id'], $node_ids)){
                    $data['node'][$key]['on'] = 2;
                }else{
                    // $data['node'][$key]['on'] = 0;
                }
            }
            $this->success('请求成功',$data);
        }
    }
    //基础节点开启
    public function nodestatus()
    {
        $node_id = $this->request->param("node_id", 0, 'intval');
        $status = $this->request->param("status", 0, 'intval');
        $type_id = $this->request->param("type_id", 0, 'intval');
        $jxsbm = $this->request->param("jxsbm", 0, 'intval');
        if (empty($status) || empty($node_id) || empty($type_id)) {
            $this->error(lang("请选择节点"));
        }
        if($status == 1)
        {
            if($this->user['gw']>6)
            {
                //获取总用户节点信息
                $bmmode = $this->rlBmNodeRelModel->where('type_id',$type_id)->where('bm',1)->find();
                $node_ids = explode(",", $bmmode['node_id']);
                $node_ids[] = $node_id;
                foreach ($node_ids as $key => $value) {
                    if(!$value){
                        unset($node_ids[$key]);
                    }
                }
                $node_ids = implode(",", $node_ids);
                //向总用户添加节点信息
                
                $result = $this->rlBmNodeRelModel->where('type_id',$type_id)->where('bm',1)->update(['node_id'=>$node_ids]);
            }elseif($this->user['gw'] > 2&&$this->user['gw'] < 7){

                //获取用户节点信息
                $bmmode = $this->rlBmNodeRelModel->where('type_id',$type_id)->where('bm',$this->user['bm_code'].'.'.$jxsbm)->find();
                $node_ids = explode(",", $bmmode['node_id']);
                $node_ids[] = $node_id;
                foreach ($node_ids as $key => $value) {
                    if(!$value){
                        unset($node_ids[$key]);
                    }
                }
                $node_ids = implode(",", $node_ids);
                //用户添加节点信息
                if(!$bmmode)
                {
                    $product_type = $this->rlTypeModel->where('type_id',$type_id)->find()['product_type'];
                    $arrData = 
                        [
                            'bm' => $this->user['bm_code'].'.'.$jxsbm,
                            'type_id' => $type_id,
                            'node_id' => $node_ids,
                            'product_type' => $product_type,
                            'create_time' => time(),
                            'update_time' => time(),
                        ];
                    $result = $this->rlBmNodeRelModel->isUpdate(false)->allowField(true)->save($arrData);
                }else{

                    $result = $this->rlBmNodeRelModel->where('type_id',$type_id)->where('bm',$this->user['bm_code'].'.'.$jxsbm)->update(['node_id'=>$node_ids]);
                }
            }else{

                //获取用户节点信息
                $bmmode = $this->rlBmNodeRelModel->where('type_id',$type_id)->where('bm',$this->user['bm_code'])->find();
                $node_ids = explode(",", $bmmode['node_id']);
                $node_ids[] = $node_id;
                foreach ($node_ids as $key => $value) {
                    if(!$value){
                        unset($node_ids[$key]);
                    }
                }
                $node_ids = implode(",", $node_ids);
                //用户添加节点信息
                if(!$bmmode)
                {
                    $product_type = $this->rlTypeModel->where('type_id',$type_id)->find()['product_type'];
                    $arrData = 
                        [
                            'bm' => $this->user['bm_code'],
                            'type_id' => $type_id,
                            'node_id' => $node_ids,
                            'product_type' => $product_type,
                            'create_time' => time(),
                            'update_time' => time(),
                        ];
                    $result = $this->rlBmNodeRelModel->isUpdate(false)->allowField(true)->save($arrData);
                }else{

                    $result = $this->rlBmNodeRelModel->where('type_id',$type_id)->where('bm',$this->user['bm_code'])->update(['node_id'=>$node_ids]);
                }
            }
        }
        if($status == 2)
        {
            if($this->user['gw']>6)
            {
                 //删除总用户节点信息
                $bmnoderel = $this->rlBmNodeRelModel->where('type_id' , $type_id)->where('bm',1)->find();
                $node_ids = explode(",", $bmnoderel['node_id']);
                foreach ($node_ids as $key => $value) {
                   if($value==$node_id)
                   {
                        unset($node_ids[$key]);
                   }
                }
                foreach ($node_ids as $key => $value) {
                    if(!$value){
                        unset($node_ids[$key]);
                    }
                }
                $node_ids = implode(",", $node_ids);
                 $result = $this->rlBmNodeRelModel->where('type_id',$type_id)->where('bm',1)->update(['node_id'=>$node_ids]);
            }elseif($this->user['gw'] > 2&&$this->user['gw'] < 7){

                $bmnoderel = $this->rlBmNodeRelModel->where('type_id' , $type_id)->where('bm',$this->user['bm_code'].'.'.$jxsbm)->find();
                $node_ids = explode(",", $bmnoderel['node_id']);
                foreach ($node_ids as $key => $value) {
                   if($value==$node_id)
                   {
                        unset($node_ids[$key]);
                   }
                }
                foreach ($node_ids as $key => $value) {
                    if(!$value){
                        unset($node_ids[$key]);
                    }
                }
                $node_ids = implode(",", $node_ids);
                 $result = $this->rlBmNodeRelModel->where('type_id',$type_id)->where('bm',$this->user['bm_code'].'.'.$jxsbm)->update(['node_id'=>$node_ids]);
            }else{
                 //用户节点信息
                $bmnoderel = $this->rlBmNodeRelModel->where('type_id' , $type_id)->where('bm',$this->user['bm_code'])->find();
                $node_ids = explode(",", $bmnoderel['node_id']);
                foreach ($node_ids as $key => $value) {
                   if($value==$node_id)
                   {
                        unset($node_ids[$key]);
                   }
                }
                foreach ($node_ids as $key => $value) {
                    if(!$value){
                        unset($node_ids[$key]);
                    }
                }
                $node_ids = implode(",", $node_ids);
                 $result = $this->rlBmNodeRelModel->where('type_id',$type_id)->where('bm',$this->user['bm_code'])->update(['node_id'=>$node_ids]);
            }
           
        }
        
        if($result){
             $this->success(lang('操作成功'));
           }else{
              $this->error(lang('操作失败'));
            }
    }
}